<?
class Util{
	public static function dadosForm($obj){
		$aux = explode('|;|',$obj);
		$post = array();
		for($i = 0; $i < (sizeof($aux)-1); $i++){
			$post[$aux[$i]] = Util::convertChartoEntities($aux[$i+1]);
			$i++;
		}
		
		return $post;
	}

	public static function typesImages($type){
		$types["image/gif"]	  = "gif";
		$types["image/pjpeg"] = "jpg";
		$types["image/jpeg"]  = "jpeg";
		$types["image/jpg"]	  = "jpg";
		$types["image/png"]	  = "png";
		$types["image/x-png"] = "png";
			
		if(!is_null($types[$type]))
			return $types[$type];
		else
			return false;
	}


	public static function trim_value(&$value){ 
		$value = trim($value); 
	}

	public static function pagination($totalRegistros, $pagAtual=1){
		$paginacao="";
		if($totalRegistros > PAGINATION){
			$paginas = (int)($totalRegistros/PAGINATION);
			if($totalRegistros%PAGINATION > 0)
				$paginas++;

			$anterior = 1;
			if($pagAtual > 1)
				$anterior = $pagAtual - 1;

			$proximo = $paginas;
			if($pagAtual < $paginas)
				$proximo = $pagAtual + 1;

			$paginacao = '<div class="pagination">';
			$paginacao .=  Util::paginationItem(1, $pagAtual, "<< Primeiro");
			$paginacao .=  " ".Util::paginationItem($anterior, $pagAtual, "< Anterior")." ";

			$contInicial	= $pagAtual>(PAGINATION_RESULT/2)?$pagAtual-(PAGINATION_RESULT/2):1;
			if($contInicial > $paginas-PAGINATION_RESULT && $paginas-PAGINATION_RESULT > 0)
				$contInicial = $paginas-PAGINATION_RESULT;

			$contFinal		= $contInicial + PAGINATION_RESULT;
			if($contFinal > $paginas)
				$contFinal = $paginas;

			for($i=$contInicial; $i <= $contFinal; $i++){
				$paginacao .= Util::paginationItem($i, $pagAtual, $i)." ";
			}

			$paginacao .=  Util::paginationItem($proximo, $pagAtual, "Proximo >")." ";
			$paginacao .=  Util::paginationItem($paginas, $pagAtual, "Último >>");			
			$paginacao .= '</div>';
		}

		return $paginacao;
	}

	public static function paginationItem($id, $pagAtual, $texto){
		if($id != $pagAtual){
			return '<a href="javascript: void(0);" onclick="addHash(\'p\','.$id.')">'.$texto.'</a>';
		}else{
			if((int)$texto > 0)
				$class = "selected";
			else
				$class = "disabled";

			return '<a href="javascript: void(0);" class="'.$class.'">'.$texto.'</a> ';
		}
	}

	public static function sendMail($fromName, $fromEmail, $listEmail, $subject, $body, $files=null, $type="", $chaveValidacao=""){		
		$boundaryFile = md5(date("dmYis")."file"); 
		$boundaryMsg  = md5(date("dmYis")."msg"); 

		//Header do e-mail
		$headers  = "From: ".$fromName." <".$fromEmail."> \n";
		$headers .= "Reply-To: ".$fromEmail." \n";
		$headers .= "X-Mailer: PHP5 \n";
		$headers .= "MIME-Version: 1.0 \n";
		$headers .= "Content-Type: multipart/mixed; boundary=\"".$boundaryFile."\"\n\n";
		
		//Estrutura da corpo do email
		$message = "--".$boundaryFile."\n"; 
		$message .= "Content-Type: multipart/alternative; boundary=\"".$boundaryMsg."\"\n\n";
		$message .= "--".$boundaryMsg."\n"; 
		$message .= "Content-Type: text/html; charset=ISO-8859-1 \n\n";
		$message .= $body."\n\n";
		$message .= "--".$boundaryMsg."--\n";
	
		if($files != null){
			$bodyFiles = "";
			foreach($files as $row){
				$bodyFiles .= "--$boundaryFile\n";
				$bodyFiles .= "Content-Type: ".substr($row->formato,1)."; name=\"".$row->titulo."\"\n";
				$bodyFiles .= "Content-Disposition: attachment; filename=\"".$row->titulo."\"\n";
				$bodyFiles .= "Content-Transfer-Encoding: base64\n\n";
				$bodyFiles .= Util::convertFile(DIRETORIO."images/".$type."/".$row->id."/".$row->id_arquivo.$row->formato)."\n";
			}
		}

		$message .= $bodyFiles;
		$message .= "--".$boundaryFile."--";
		
		if(is_array($listEmail)){
			$count = 0;
			foreach($listEmail as $row){
				if($chaveValidacao != ""){
					$removeMail = base64_encode(md5("GA-->".$chaveValidacao).",".$chaveValidacao.",".$row);
					$bodyMail	= str_replace("[REMOVE_MAIL]",SITE."content/home/email.php?".$removeMail,$message);
				}else{
					$bodyMail	= $message;
				}
				if(mail($row, html_entity_decode($subject), $bodyMail, $headers))
					$count++;
			}
			return $count;
		}else{
			return mail($listEmail, html_entity_decode($subject), $message, $headers);
		}
	}

	public static function convertFile($file){
		$fileTemp	= file_get_contents($file);
		$fileTemp	= base64_encode($fileTemp);
		return chunk_split($fileTemp); 
	}

	public static function convertChartoEntities($text=""){
		$trans			 = get_html_translation_table(HTML_ENTITIES);
		$trans['&']		 = '&';
		$trans[chr(130)] = '&sbquo;';		// Single Low-9 Quotation Mark
		$trans[chr(131)] = '&fnof;';		// Latin Small Letter F With Hook
		$trans[chr(132)] = '&bdquo;';		// Double Low-9 Quotation Mark
		$trans[chr(133)] = '&hellip;';		// Horizontal Ellipsis
		$trans[chr(134)] = '&dagger;';		// Dagger
		$trans[chr(135)] = '&Dagger;';		// Double Dagger
		$trans[chr(136)] = '&circ;';		// Modifier Letter Circumflex Accent
		$trans[chr(137)] = '&permil;';		// Per Mille Sign
		$trans[chr(138)] = '&Scaron;';		// Latin Capital Letter S With Caron
		$trans[chr(139)] = '&lsaquo;';		// Single Left-Pointing Angle Quotation Mark
		$trans[chr(140)] = '&OElig;';		// Latin Capital Ligature OE
		$trans[chr(145)] = '&lsquo;';		// Left Single Quotation Mark
		$trans[chr(146)] = '&rsquo;';		// Right Single Quotation Mark
		$trans[chr(147)] = '&ldquo;';		// Left Double Quotation Mark
		$trans[chr(148)] = '&rdquo;';		// Right Double Quotation Mark
		$trans[chr(149)] = '&bull;';		// Bullet
		$trans[chr(150)] = '&ndash;';		// En Dash
		$trans[chr(151)] = '&mdash;';		// Em Dash
		$trans[chr(152)] = '&tilde;';		// Small Tilde
		$trans[chr(153)] = '&trade;';		// Trade Mark Sign
		$trans[chr(154)] = '&scaron;';		// Latin Small Letter S With Caron
		$trans[chr(155)] = '&rsaquo;';		// Single Right-Pointing Angle Quotation Mark
		$trans[chr(156)] = '&oelig;';		// Latin Small Ligature OE
		$trans[chr(159)] = '&Yuml;';		// Latin Capital Letter Y With Diaeresis

		if($text != "")
			$text = strtr($text, $trans);
		
		return $text;
	}

	public static function encodeText($text) {		
		$text = addslashes($text);
		$text = trim($text);

		return($text);
	}

	public static function decodeText($text) {
		$text = html_entity_decode($text);

		return($text);
	}

	public static function stringToEmail($var) {
		$var = strtolower($var);
		$var = ereg_replace("[áàâãª]","a",$var);	
		$var = ereg_replace("[éèê]","e",$var);	
		$var = ereg_replace("[óòôõº]","o",$var);	
		$var = ereg_replace("[úùû]","u",$var);	
		$var = str_replace("ç","c",$var);
		
		return $var;
	}

	public static function getNameMonth($month){
		$arrMonth = array("Janeiro","Fevereiro","Março","Abril","Maio","Junho","Julho","Agosto","Setembro","Outubro","Novembro","Dezembro");
		if($month > 0)
			return $arrMonth[$month - 1];
		else
			return "";
	}

	public static function getNameWeek($dayName){
		$arrWeek["Sunday"]		= "Domingo";
		$arrWeek["Monday"]		= "Segunda-feira";
		$arrWeek["Tuesday"]		= "Terça-feira";
		$arrWeek["Wednesday"]	= "Quarta-feira";
		$arrWeek["Thursday"]	= "Quinta-feira";
		$arrWeek["Friday"]		= "Sexta-feira";
		$arrWeek["Saturday"]	= "Sábado";
		if($dayName != "")
			return $arrWeek[$dayName];
		else
			return "";
	}
}
?>